package karaoke.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import karaoke.excepcion.DAOExcepcion;
import karaoke.json.CustomEventoResponse;
import karaoke.json.CustomGenericResponse;
import karaoke.json.CustomReservaResponse;
import karaoke.modelo.Cliente;
import karaoke.modelo.Evento;
import karaoke.modelo.Local;
import karaoke.modelo.Reserva;
import karaoke.negocio.GestionCliente;
import karaoke.negocio.GestionEvento;
import karaoke.negocio.GestionLocal;
import karaoke.negocio.GestionReserva;
import karaoke.negocio.GestionSala;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("evento")
public class EventoController {

	@RequestMapping(method = RequestMethod.GET)
    public String obtenerPaginaEvento() {
    	return "/mantenimiento/evento";
	}
	
	
	@RequestMapping(value="/lista", method = RequestMethod.GET)
    public @ResponseBody CustomEventoResponse listaEvento(){
		CustomEventoResponse res= new CustomEventoResponse();
		 GestionEvento gestion = new GestionEvento();
		 
		 try {
			List<Evento> lstEvento= gestion.buscarPorNombre("");
			res.setRows(lstEvento);
			res.setRecords( String.valueOf(lstEvento.size()) );
			res.setPage( "1" );
        	
        	// Same. Assign a dummy total pages
			res.setTotal( "10" );
        	
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return res; 
	 }
	
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody CustomGenericResponse add(Evento evento) {
    	
		GestionEvento gestion = new GestionEvento();
		 try {
			 evento = gestion.insertar(evento.getNombre(),evento.getFecha(),evento.getCapacidad(),evento.getDescripcion(),evento.getEstado(), evento.getLocal().getIdLocal(),evento.getCliente().getIdCliente());
    	
    	// Check if successful
    	if ( evento.getIdevento()>0 ) {
    		// Success. Return a custom response
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(true);
			response.setMessage("Action successful!");
    		return response;
    		
    	} else {
    		// A failure. Return a custom response as well
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(false);
			response.setMessage("Action failure!");
    		return response;
    	}
		 } catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				CustomGenericResponse response = new CustomGenericResponse();
				response.setSuccess(false);
				response.setMessage("Action failure!");
	    		return response;
	    		// :(
				
			}
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
    public @ResponseBody CustomGenericResponse edit(
    		@RequestParam("id") String id,
    		Evento evento) {
    	
		GestionEvento gestion = new GestionEvento();
		 try {
			
			 evento.setIdevento(Integer.valueOf(id));
			 evento=gestion.actualizar(evento.getIdevento(),evento.getNombre(),evento.getFecha(),evento.getCapacidad(),evento.getDescripcion(),evento.getLocal().getIdLocal(),evento.getCliente().getIdCliente());
    	
    	
    	// Check if successful
    	if ( evento.getIdevento() !=0 ) {
    		// Success. Return a custom response
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(true);
			response.setMessage("Action successful!");
    		return response;
    		
    	} else {
    		// A failure. Return a custom response as well
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(false);
			response.setMessage("Action failure!");
    		return response;
    	}
		 } catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	}
	
	
	@RequestMapping(value="/listaTodo", method = RequestMethod.GET)
    public @ResponseBody Collection<Cliente>  listaTodo(){
		 
		 GestionCliente gestion = new GestionCliente();
		 Collection<Cliente> lstCliente=new  ArrayList<Cliente>();
		 try {
			 lstCliente= gestion.listar();
        	
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			lstCliente= new ArrayList<Cliente>();
		}
		 
		return lstCliente; 
	 }
	
	
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody CustomGenericResponse delete(
    		@RequestParam("id") String id
    ) {
    	
    	
    	GestionEvento gestion= new GestionEvento();
    	try{
    		gestion.eliminar(Integer.valueOf(id));
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(true);
			response.setMessage("Action successful!");
    		return response;
    		
	 } catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(false);
			response.setMessage("Action failure!");
    		return response;
		}
    		
    	
	}
}
